package replacer;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import net.miginfocom.swing.MigLayout;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.JCheckBox;

public class Main {

	private JFrame frame;
	private JTextArea txtrInput;
	private Engine engine;
	
	private DefaultListModel<String> suggestions = new DefaultListModel<String>();
	private JCheckBox chckbxMissingLetters;
	private JCheckBox chckbxExtraLetters;
	private JCheckBox chckbxSwitchedLetters;
	private JCheckBox chckbxMisspelling;
	private JCheckBox chckbxTypo;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException | UnsupportedLookAndFeelException e1) {
			e1.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main window = new Main();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Main() {
		initialize();
		initializeEngine();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setTitle("Word Replacer");
		frame.setBounds(100, 100, 493, 367);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new MigLayout("", "[grow]", "[60%][40%][grow]"));
		
		// Text Area
		txtrInput = new JTextArea();
		txtrInput.setFont(new Font("Serif", Font.PLAIN, 12));
		txtrInput.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				super.mouseClicked(e);
				if (e.getButton() == MouseEvent.BUTTON1) {
					int caretPos = txtrInput.getCaretPosition();
					String content = txtrInput.getText();
					int startPos = content.lastIndexOf(' ', caretPos)+1;
					int endPos = content.indexOf(' ', caretPos);
					if (endPos == -1)
						endPos = content.length();
					if (endPos-startPos < 1)
						return;
					else
						updateSuggestionsList(content.substring(startPos, endPos));
				}
			}
			
		});
		
		JScrollPane inputScrollPane = new JScrollPane(txtrInput);
		inputScrollPane.setBorder(BorderFactory.createTitledBorder("Input Text"));
		frame.getContentPane().add(inputScrollPane, "cell 0 0,grow");
		
		// List Area
		JList<String> list = new JList<String>();
		list.setModel(suggestions);
		
		JScrollPane suggestionsScrollPane = new JScrollPane(list);
		suggestionsScrollPane.setBorder(BorderFactory.createTitledBorder("Suggestions"));
		frame.getContentPane().add(suggestionsScrollPane, "cell 0 1,grow");
		
		JPanel panelOptions = new JPanel();
		panelOptions.setBorder(new TitledBorder(null, "Options", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		frame.getContentPane().add(panelOptions, "cell 0 2,grow");
		panelOptions.setLayout(new MigLayout("", "[grow,center][grow,center][grow,center][grow,center][grow,center]", "[][]"));
		
		chckbxMissingLetters = new JCheckBox("Missing letter(s)");
		chckbxMissingLetters.setSelected(true);
		panelOptions.add(chckbxMissingLetters, "cell 0 0");
		
		chckbxExtraLetters = new JCheckBox("Extra letter(s)");
		chckbxExtraLetters.setSelected(true);
		panelOptions.add(chckbxExtraLetters, "cell 1 0");
		
		chckbxSwitchedLetters = new JCheckBox("Switched letters");
		chckbxSwitchedLetters.setSelected(true);
		panelOptions.add(chckbxSwitchedLetters, "cell 2 0");
		
		chckbxMisspelling = new JCheckBox("Misspelling");
		chckbxMisspelling.setSelected(true);
		panelOptions.add(chckbxMisspelling, "cell 3 0");
		
		chckbxTypo = new JCheckBox("Typo");
		chckbxTypo.setSelected(true);
		panelOptions.add(chckbxTypo, "cell 4 0");
	} 
	
	private void initializeEngine() {
		
		try {
			engine = new Engine();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	private void updateSuggestionsList(String keyword) {
//		suggestions.clear();
		engine.suggest(keyword, suggestions, getCheckboxesValues());
//		suggestions.addElement(keyword);
	}
	
	private boolean[] getCheckboxesValues() {
		boolean result[] = new boolean[5];
		result[0] = chckbxMissingLetters.isSelected();
		result[1] = chckbxExtraLetters.isSelected();
		result[2] = chckbxSwitchedLetters.isSelected();
		result[3] = chckbxMisspelling.isSelected();
		result[4] = chckbxTypo.isSelected();
		return result;
	}
	
	

}
